﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BlackBBS.Models.Data;
using BlackBBS.Infrastructure.Logging;
using BlackBBS.Infrastructure.TelnetProtocol;
using BlackBBS.Infrastructure.Repository;

namespace BlackBBS.Menus
{
    public abstract class MenuBase
    {
        /// <summary>
        /// Telnet client active for the connected client
        /// </summary>
        public Connection Connection { get; set; }
        public Dictionary<string, string> Keys { get; set; }

        public bool DoImmediateDisconnect { get; set; }

        public virtual void Init()
        {
            if (this.Connection == null)
            {
                throw new Exception("Invalid Connection");
            }
        }

        public abstract void SendMenu();

        internal void LogDebug(string message)
        {
            if (this.Connection.Logger != null)
            {
                this.Connection.Logger.Debug(message);
            }
        }

        internal void LogException(Exception ex)
        {
            if (this.Connection.Logger != null)
            {
                this.Connection.Logger.Error(ex);
            }
        }
        internal void LogException(string message)
        {
            if (this.Connection.Logger != null)
            {
                this.Connection.Logger.Error(message);
            }
        }

        internal void DoLogOff(bool doImmediateDisconnect)
        {
            // Disconnect
            var logOff = new BlackBBS.Menus.LogOff
            {
                Connection = this.Connection,
                DoImmediateDisconnect = doImmediateDisconnect
            };
            logOff.Init();
            logOff.SendMenu();
        }

        internal void SendToMain()
        {
            var main = new BlackBBS.Menus.Main
            {
                Connection = this.Connection
            };
            main.Init();
            main.SendMenu();
        }
    }
}
